import { _decorator } from "cc";
import { $planet_DataManager_poof$ } from "../ALIB/lib.data";
import { $planet_UI_poof$ } from "./ui.origin";

const {ccclass, property} = _decorator;

@ccclass
export abstract class $planet_BaseUI_poof$ extends $planet_UI_poof${
    protected $planet___listen_list___poof$: ReturnType<typeof this.$planet_listening_poof$> = [];

    /** 注册监听 */
    protected $planet_listening_poof$(): ReturnType<typeof $planet_DataManager_poof$.$planet_follow_poof$>[] {return []};
    /** 取消监听 */
    protected $planet_closeListen_poof$(){
        this.$planet___listen_list___poof$.forEach(reg=>$planet_DataManager_poof$.$planet_cancel_poof$(reg));
		abd.$planet_array_poof$.$planet_clear_poof$(this.$planet___listen_list___poof$);
    };

    protected onLoad(): void {
		this.$planet___ui_loaded_poof$ = false;
        super.onLoad();
        this.$planet___listen_list___poof$ = this.$planet_listening_poof$();
        this.$planet___ui_loaded_poof$ = true;
        this.$planet_call_cache_poof$.forEach(el=>{
            let [func, param, np] = el;
			np.$planet_resolve_poof$(Reflect.apply(func, this, param));
        });
		abd.$planet_array_poof$.$planet_clear_poof$(this.$planet_call_cache_poof$);
    }

    protected onDestroy(){
        this.$planet_closeListen_poof$();
    }

	private $planet___ui_loaded_poof$: boolean;
	/** ui加载状态 */
    protected get $planet_ui_loaded_poof$(){
		return this.$planet___ui_loaded_poof$;
	};
    private $planet_call_cache_poof$: [Function, any[], abd.$planet_NudityPromise_poof$<any>][] = [];
    protected $planet_call_func_poof$<F extends (...param: any)=>any, R extends ReturnType<F>>(func: F, ...param: Parameters<F>): R|Promise<R>{
        if(this.$planet_ui_loaded_poof$) return Reflect.apply(func, this, param);
        else{
			let np = new abd.$planet_NudityPromise_poof$<R>();
			this.$planet_call_cache_poof$.push([func, param, np]);
			return np.$planet_promise_poof$;
		}
    }

	private $planet___late_call_list___poof$: Array<[Function, any[], Function]> = [];
	protected lateUpdate(dt: number): void {
		if(this.$planet___late_call_list___poof$.length>0){
			let [call, args, complete] = this.$planet___late_call_list___poof$.pop();
			typeof complete=='function' ? complete(call(...args), args) : call(...args);
		}
	}

	/**
	 * 在帧后执行回调
	 * @param call
	 * @param args
	 * @param complete
	 */
	protected $planet_frameLateCall_poof$<C extends (...args: any[])=>any, Param extends Parameters<C>, R extends ReturnType<C>>(call: C, args: Param, complete?: (r: R, args: Param)=>void){
		this.$planet___late_call_list___poof$.unshift([call, args, complete]);
	}

	/**
	 * 帧循环
	 * @param list
	 * @param call
	 * @returns
	 */
	protected $planet_frameWhile_poof$<T extends any[]>(list: T, call: (el: T[number], index: number, arr: T)=>void){
		return new Promise<0>(s=>list.forEach((e, i, a)=>this.$planet_frameLateCall_poof$(call, [e, i, list], (_, [e, i, a])=>i==list.length-1 ? s(0) : void 0)));
	}
}